﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Media3D;
using HelixToolkit.Wpf;

namespace GameBoard
{
    public class ModelVisual :  ModelVisual3D, ICharacterVisual<ModelVisualData>
    {
        private bool _belongsToMoveablePicture;

        private ScaleTransform3D _scaleTransform;
        private RotateTransform3D _rotateTransform;
        private TranslateTransform3D _translateTransform;

        public void PopulateVisualData(BaseVisualData theData)
        {
            _belongsToMoveablePicture = theData.BelongsToMoveablePicture;
            FactoryID = theData.FactoryID;
            var objData = theData as ModelVisualData;
            Content = MakeModelVisual3DFromObjectFile(objData.ObjectFile, objData.Material).Content;
            
            _scaleTransform = objData.ScaleTransform;
            _rotateTransform = objData.RotateTransform;
            _translateTransform = objData.TranslateTransform;

            Transform = FindCurrentTransform();
        }

        private Transform3D FindCurrentTransform()
        {
            Transform3DGroup t = new Transform3DGroup();
            t.Children.Add(_rotateTransform);
            t.Children.Add(_scaleTransform);
            t.Children.Add(_translateTransform);
            AdjustTransformForConeHeight(_translateTransform);
            return t;
        }

        private void AdjustTransformForConeHeight(TranslateTransform3D translateTransform)
        {
            translateTransform.OffsetZ += MoveablePicture.ConeHeight;
        }


        private ModelVisual3D MakeModelVisual3DFromObjectFile(string modelPath, Material frontMaterial)
        {
            var objReader = new ObjReader();
            var group = objReader.Read(modelPath);
            ModelVisual3D myModelVisual3D = new ModelVisual3D();
            myModelVisual3D.Content = group;
            string materialFile = modelPath.Replace(".obj", ".mtl");
            if (!File.Exists(materialFile))
            {
                foreach (GeometryModel3D geometry in (myModelVisual3D.Content as Model3DGroup).Children)
                {
                    geometry.Material = frontMaterial;
                }
            }

            return myModelVisual3D;
        }


        public ModelVisual3D VisualElement
        {
            get { return this; }
        }

        public Guid FactoryID { get; set; }

        public bool BelongsToMoveablePicture
        {
            get { return _belongsToMoveablePicture; }
        }

        public void MoveTo(Point3D exactPoint)
        {
            _translateTransform = new TranslateTransform3D(exactPoint.X, exactPoint.Y, exactPoint.Z);
            Transform = FindCurrentTransform();
            //var duration = new Duration(TimeSpan.FromSeconds(1));
            //DoubleAnimation transformAnimationX = new DoubleAnimation();
            //var moveAnimationPic =
            //    new Point3DAnimation(Origin, exactPoint, duration);

            //// Create the animation path.
            //PathGeometry animationPath = new PathGeometry();

            //// Create a MatrixAnimationUsingPath to move the 
            //// button along the path by animating 
            //// its MatrixTransform.
            //MatrixAnimationUsingPath matrixAnimation =
            //    new MatrixAnimationUsingPath();
            //matrixAnimation.PathGeometry = animationPath;
            //matrixAnimation.Duration = TimeSpan.FromSeconds(5);
            //matrixAnimation.RepeatBehavior = RepeatBehavior.Forever;
        }
    }
}
